package com.selfdriving.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.widget.Toast;

import com.selfdriving.BaseApplication;
import com.selfdriving.bean.User;
import com.selfdriving.dialog.FlippingLoadingDialog;

public class Utils
{
	public static FlippingLoadingDialog mLoadingDialog;

	public static Date dateFormat(String st)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date da = new Date();
		try
		{
			da = sdf.parse(st);
		} catch (ParseException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return da;
	}

	public static String getAge(Date birthDay) throws Exception
	{
		Calendar cal = Calendar.getInstance();

		if (cal.before(birthDay))
		{
			throw new IllegalArgumentException(
					"The birthDay is before Now.It's unbelievable!");
		}

		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH) + 1;
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

		cal.setTime(birthDay);
		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH);
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

		int age = yearNow - yearBirth;

		if (monthNow <= monthBirth)
		{
			if (monthNow == monthBirth)
			{
				// monthNow==monthBirth
				if (dayOfMonthNow < dayOfMonthBirth)
				{
					age--;
				}
			} else
			{
				// monthNow>monthBirth
				age--;
			}
		}
		return age + "";
	}

	public static void dismissLoadingDialog()
	{
		if (mLoadingDialog != null && mLoadingDialog.isShowing())
		{
			mLoadingDialog.dismiss();
		}
	}

	public static void showLoadingDialog(Context context, String text)
	{
		mLoadingDialog = new FlippingLoadingDialog(context, "请求提交中");
		if (text != null)
		{
			mLoadingDialog.setText(text);
		}
		mLoadingDialog.show();
	}

	/** 短暂显示Toast提示(来自String) **/
	public static void showShortToast(Context context, String text)
	{
		Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
	}

	/**
	 * 检查是否可登录
	 * 
	 * @param context
	 */
	public static void checkLoading(BaseApplication application, Context context)
	{
		NetworkCheck networkCheck = new NetworkCheck(context);
		if (application.user == null)
		{
			String accountValue = SystemSettings.getString(context, "phone");
			String passwordValue = SystemSettings.getString(context, "pwd");
			if (accountValue != null && !accountValue.equals("0")
					&& networkCheck.isNetworkConnected())
			{
				loading(application, context, accountValue, passwordValue);
			}
		}

	}

	public static void loading(final BaseApplication application,
			final Context context, final String phone, final String pwd)
	{
		AjaxParams params = new AjaxParams();
		params.put("f", "13");
		params.put("phone", phone);
		params.put("pwd", pwd);
		params.put("img", "http://117.21.209.117:9000/driving/headimg/user.jpg");
		params.put("lat", "" + application.mLatitude);
		params.put("lng", "" + application.mLongitude);
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				super.onFailure(t, strMsg);
			}

			@Override
			public void onSuccess(Object t)
			{
				super.onSuccess(t);
				String json = t.toString();
				if (json.contains("000000"))
				{
					try
					{
						JSONObject object = new JSONObject(json);
						String success = object.getString("result");
						if (success.equals("0"))
						{
							String name = object.getString("name");
							String icon = object.getString("img");
							application.user = new User(icon, phone, name, pwd,
									application.mLatitude,
									application.mLongitude);
							SystemSettings.putString(context, "phone", phone);
							SystemSettings.putString(context, "pwd", pwd);
						}
					} catch (JSONException e)
					{
					}

				}
			}
		});
	}

	public static String transitionDate(String st)
	{
		String date = null;
		Date now = new Date();
		Date stDate = dateFormat(st);
		if (now.getYear() != stDate.getYear())
		{
			date = dateFormat(stDate, "yyyy-MM-dd");
		} else
		{
			date = dateFormat(stDate, "MM-dd HH:mm");
		}
		return date;
	}

	public static String dateFormat(Date date, String type)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(type);
		return sdf.format(date);
	}

	public static boolean isNullOrEmpty(String s)
	{
		return ((s == null) || ("".equals(s.trim()))) || "null".equals(s);
	}
}
